package com.tools.genxsd.store;

import cn.hutool.core.text.CharSequenceUtil;
import com.tools.genxsd.entity.XElement;
import com.tools.genxsd.entity.XsdElement;
import com.tools.genxsd.entity.type.XsdComplexType;
import com.tools.genxsd.util.ExcelUtil;
import com.tools.genxsd.util.FilePathUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * 组件库
 *
 * @author: zhoubin
 * @date: 2022/11/12
 */
public class TempElementStore {

    public static Map<String, XsdElement> xsdElementMap  = new HashMap();;

    static {

        String baseElementPath = FilePathUtil.getBaseElementFile();
        File baseEleFile = null;
        try {
            if (CharSequenceUtil.isNotBlank(baseElementPath) && (baseEleFile = new File(baseElementPath)).exists()) {
                praseTemXsdElementList(baseEleFile);
            }
        } catch (Exception e) {
            System.out.println("文件: " + baseElementPath + "的公共组件生成失败");
            e.printStackTrace();
        }
    }

    public static XsdComplexType getXsdElementType(String name) {

        if (name.startsWith("【")) {
            String eleName = name.substring(1, name.length() - 1);

            XsdElement element = xsdElementMap.get(eleName);

            if (Objects.isNull(element)) {
                throw new IllegalArgumentException("无该类型的基础组件: " + name);
            }

            return element.getXsdType();
        }

        throw new IllegalArgumentException("获取基础组件的参数错误，需要【组件名】, 输入: " + name);
    }

    public static void praseTemXsdElementList(File file) throws Exception {

        ExcelUtil.praseExcel(file, workbook -> {

            XSSFSheet sheet = workbook.getSheet("公共组件");

            if (Objects.isNull(sheet)) {
                System.out.println("文件：" + file.getName() + "中名为 公共组件 的sheet不存在");
                return;
            }

            for (int i = 0; ; i++) {
                XSSFRow row = sheet.getRow(i);

                if (Objects.isNull(row)) {
                    if (Objects.isNull(sheet.getRow(i - 1))) {
                        break;
                    }
                    continue;
                }
                XSSFCell cell = row.getCell(0);

                if (Objects.nonNull(cell)
                        && isTemTitle(cell.toString())
                        && !xsdElementMap.containsKey(cell.toString().trim())) {

                    XElement xElement = new XElement(sheet.getRow(i + 2));
                    xElement.setIndex(i + 2);

                    XsdElement element = new XsdElement();
                    element.setName(xElement.getTag());
                    element.setMark(xElement.getName());
                    element.setMaxOccurs(xElement.getMaxOccurs());
                    element.setMinOccurs(xElement.getMinOccurs());
                    element.setType(xElement.getType());
                    ExcelUtil.creatXsdElement(element, sheet, xElement);

                    xsdElementMap.put(cell.toString().trim(), element);

                    i = i + 2;
                }
            }
        });
    }

    /**
     * 判断是否是标题
     * @param title
     * @return
     */
    public static boolean isTemTitle(String title) {

        if (CharSequenceUtil.isBlank(title)) {
            return false;
        }

        String regex = "[\\u4e00-\\u9fa5]+";
        Pattern compile = Pattern.compile(regex);

        return compile.matcher(title.trim()).matches();
    }
}
